package com.zone.vehicle.superapp.view.util;

import android.annotation.SuppressLint;
import android.util.Log;

import com.zone.lib.download.utils.DateUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UnknownFormatConversionException;

public class TimeUtils {

    public static String getRecentTimeSpanByNow(final long millis) {
        try {
            long now = System.currentTimeMillis();
            long span = now - millis;
            if (span < TimeConstants.HOUR) {
                return "刚刚";
            } else if (span < TimeConstants.DAY) {
                int hours = (int)(span / TimeConstants.HOUR);
                if (hours < 4){
                    return String.format(Locale.getDefault(), "%d小时前", span / TimeConstants.HOUR);
                }else {
                    return "今天";
                }
            } else if (span < TimeConstants.MONTH) {
                int days = (int)(span / TimeConstants.DAY);
                if (days == 1){
                    return "昨天";
                }else if (days == 2){
                    return "前天";
                }else {
                    String date = DateUtils.getLongToString(millis, DateUtils.DATE_TO_STRING_SHORT_PATTERN);
                    return date.substring(5);
                }
            } else if (span < TimeConstants.YEAR) {
                String date = DateUtils.getLongToString(millis, DateUtils.DATE_TO_STRING_SHORT_PATTERN);
                return date.substring(5);
            } else if (span > TimeConstants.YEAR) {
                String date = DateUtils.getLongToString(millis, DateUtils.DATE_TO_STRING_SHORT_PATTERN);
                return String.format(Locale.getDefault(), "%", date);
            }
        }catch (UnknownFormatConversionException e){
            Log.e("TimeUtils", "time="+e.toString());
        }

        return null;
    }

    public static String getTimeSpanByNow(final long millis) {
        long now = System.currentTimeMillis();
        long span = now - millis;
        if (span < 1000) {
            return "刚刚";
        } else if (span < TimeConstants.MIN) {
            return String.format(Locale.getDefault(), "%d秒前", span / TimeConstants.SEC);
        } else if (span < TimeConstants.HOUR) {
            return String.format(Locale.getDefault(), "%d分钟前", span / TimeConstants.MIN);
        }else if (span < TimeConstants.DAY) {
            return String.format(Locale.getDefault(), "%d小时前", span / TimeConstants.HOUR);
        } else if (span < TimeConstants.MONTH) {
            return String.format(Locale.getDefault(), "%d天前", span / TimeConstants.DAY);
        } else if (span < TimeConstants.YEAR) {
            return String.format(Locale.getDefault(), "%d月前", span / TimeConstants.MONTH);
        } else if (span > TimeConstants.YEAR) {
            return String.format(Locale.getDefault(), "%d年前", span / TimeConstants.YEAR);
        } else {
            return String.format("%tF", millis);
        }
    }

    /**
     * 根据毫秒时间戳来格式化字符串
     * 今天显示今天、昨天显示昨天、前天显示前天.
     * 早于前天的显示具体年-月-日，如2017-06-12；
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    @SuppressLint("SimpleDateFormat")
    public static String formats(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        long span = curTimeMillis - timeStamp;
        if(timeStamp >= todayStartMillis) {
            if (span < TimeConstants.HOUR) {
                return "刚刚";
            } else if (span < TimeConstants.DAY) {
                int hours = (int)(span / TimeConstants.HOUR);
                if (hours < 4){
                    return String.format(Locale.getDefault(), "%d小时前", span / TimeConstants.HOUR);
                }else {
                    return "今天";
                }
            }
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if(timeStamp >= yesterdayStartMilis) {
            return "昨天";
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if(timeStamp >= yesterdayBeforeStartMilis) {
            return "前天";
        }
        if (span < TimeConstants.YEAR) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            return  sdf.format(new Date(timeStamp));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return  sdf.format(new Date(timeStamp));
        }
    }

    /**
     * 根据毫秒时间戳来格式化字符串
     * 今天显示今天、昨天显示昨天、前天显示前天.
     * 早于前天的显示具体年-月-日，如2017-06-12；
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    public static String format(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        if(timeStamp >= todayStartMillis) {
            return "今天";
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if(timeStamp >= yesterdayStartMilis) {
            return "昨天";
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if(timeStamp >= yesterdayBeforeStartMilis) {
            return "前天";
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(new Date(timeStamp));
    }

    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     * @param long_time
     * @return
     */
    public static String getTimeStateNew(String long_time){
        String long_by_13="1000000000000";
        String long_by_10="1000000000";
        if(Long.valueOf(long_time)/Long.valueOf(long_by_13)<1){
            if(Long.valueOf(long_time)/Long.valueOf(long_by_10)>=1){
                long_time=long_time+"000";
            }
        }
        Timestamp time=new Timestamp(Long.valueOf(long_time));
        Timestamp now=new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//    System.out.println("传递过来的时间:"+format.format(time));
//    System.out.println("现在的时间:"+format.format(now));
        long day_conver=1000*60*60*24;
        long hour_conver=1000*60*60;
        long min_conver=1000*60;
        long time_conver=now.getTime()-time.getTime();
        long temp_conver;
//    System.out.println("天数:"+time_conver/day_conver);
        if((time_conver/day_conver)<3){
            temp_conver=time_conver/day_conver;
            if(temp_conver<=2 && temp_conver>=1){
                return temp_conver+"天前";
            }else{
                temp_conver=(time_conver/hour_conver);
                if(temp_conver>=1){
                    return temp_conver+"小时前";
                }else {
                    temp_conver=(time_conver/min_conver);
                    if(temp_conver>=1){
                        return temp_conver+"分钟前";
                    }else{
                        return "刚刚";
                    }
                }
            }
        }else{
            return format.format(time);
        }
    }


}
